//
//  BrowseHistoryList.swift
//  YoruNeko
//
//  Created by  mahoPunch on 2021/6/24.
//

import SwiftUI

struct BrowseHistoryList: View {
    
    typealias onRowFetch = (_ id: UUID) -> Void
    
    @Binding var isSelectMode: Bool
    @Binding var selectedList:Set<History>
    
    var onDeleteRow: onRowFetch?
    var onRowClick: onRowFetch?
    var callback: BrowseHistoryCallback?
    var _historyGroup:[[History]] = []
    

    
    let historyHeader = ["今天","一周内","更久以前"]
    @State var historyExpanded = [Bool].init(repeating: true, count: 3)
    
    
    var body: some View {
        
        Group{
                ScrollView {
                    VStack(alignment: .leading) {
                        ForEach(_historyGroup.indices, id:\.self) { index in
                            
                            DisclosureGroup(isExpanded: $historyExpanded[index]) {
                                ForEach(_historyGroup[index], id: \.self){
                                    history in
                                    BrowseHistoryRow(history: history, canRowBeSelected: $isSelectMode, isSelected: selectedList.contains(history), onDeleteClickClosure: { idOfHistoryToBeDeleted in
                                        onDeleteRow?(idOfHistoryToBeDeleted)
                                    })
                                    //                .transition(.slide)
                                    //                .animation(.easeInOut)
                                    .onTapGesture {
                                        if isSelectMode{
                                            if selectedList.contains(history){
                                                selectedList.remove(history)
                                            }
                                            else {
                                                selectedList.insert(history)
                                            }
                                        }
                                        else{
                                            onRowClick?(history.id)
                                        }
                                    }
                                    .onLongPressGesture {
                                        if !isSelectMode{
                                            selectedList.removeAll()
                                            isSelectMode = true
                                        }
                                    }
                                }
                            } label: {
                                Text(historyHeader[index])
                                    .onTapGesture {
                                        withAnimation {
                                            historyExpanded[index].toggle()
                                        }
                                    }
                                    .padding(5)
                                    .background(RoundedRectangle(cornerRadius: 5).foregroundColor(Color.init(.systemGray5)))
                                    .padding(.leading, 5)
                            }
                            
                            
                            
                            
                            
                        }
                    }
                    .padding(.horizontal, 5)
                    
                }
            }

        
   
        
    }
    

}
//var histories: [History] = [
//    test_BrowseHistory(date_added: "加入时间", last_visited: "最后访问时间", name: "google", url: "www.google.com"),
//    test_BrowseHistory(date_added: "加入时间", last_visited: "最后访问时间", name: "google2", url: "www.google.com"),
//    test_BrowseHistory(date_added: "加入时间", last_visited: "最后访问时间", name: "google3", url: "www.google.com"),
//    test_BrowseHistory(date_added: "加入时间", last_visited: "最后访问时间", name: "google4", url: "www.google.com")
//]
//struct BrowseHistoryList_Previews: PreviewProvider {
//    static var previews: some View {
//        BrowseHistoryList(histories: histories)
//    }
//}
